<?php

namespace App\Http\Controllers\Plugin;


use App\Helpers\ResponseData;
use Illuminate\Http\Request;
use Intervention\Image\ImageManagerStatic as Image;
use Illuminate\Support\Facades\Storage;
trait LocalUploadController
{
    public function upload(Request $request)
    {
        if ($request->hasFile('file') && $request->file('file')->isValid()) {
            $photo = $request->file('file');
            Storage::makeDirectory('public/goods/images');
            $file_name = uniqid();
            //存储本地
            $photo->storeAs('public/goods/images',$file_name.'.'.$photo->getClientOriginalExtension());
            $file_path = public_path('storage/goods/images');
            $thumbnail_file_path = $file_path . '/'.$file_name.'-300'.'.'.$photo->getClientOriginalExtension();
            //生成缩略图
            Image::make($photo)->resize(300, 300, function ($constraint) {$constraint->aspectRatio();})->save($thumbnail_file_path);
            $img['source'] = 'storage/goods/images/'.$file_name.'.'.$photo->getClientOriginalExtension();
            $img['thumb']  = 'storage/goods/images/'.$file_name.'-300'.'.'.$photo->getClientOriginalExtension();
            return $img;
        }else{
            dd('未上传任何文件');
        }
    }
}
